package com.alittle.core;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.alittle.support.support.mvp.MvpPresenter;
import com.alittle.support.support.mvp.MvpView;

public abstract class ALBaseMvpFragment<P extends MvpPresenter> extends ALBaseFragment implements MvpView {

    protected P presenter;

    protected abstract P createPresenter();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (presenter == null) {
            presenter = createPresenter();
            if (presenter == null) {
                throw new NullPointerException("Presenter is null! Do you return null in createPresenter()?");
            }
        }
        presenter.attachView(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.detachView(getRetainInstance());
    }

}
